<?php
namespace app\index\controller;

use app\common\model\CompanyGongdeng as CompanyGongdengModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;

/**
 * 寺院供灯控制器
 * Class Index
 * @package app\index\controller
 */
class Companygongdeng extends Base
{
    /**
     * [index  供灯规格]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $user = $this->user;
        $model = new CompanyGongdengModel;
        $gongdeng = config('gongdeng');
        try {
            $company = $model->findData($user['comid'], 'comid', 'standard,name,days,donor_num');
            if(!empty($company)){
                $company['standard'] = unserialize($company['standard']);
            }else{
                $company = $gongdeng;
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '查看寺院心灯规格');
        return admin_json(1, '获取成功', $company);
    }
    /**
     * [edit  编辑]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function edit()
    {
        $model = new CompanyGongdengModel;
        $param = input('param.');
        $user = $this->user;
        try{
            $gongdeng = $model->findData($user['comid'],'comid','comid');
            if(empty($gongdeng)){
                $param['standard'] = serialize($param['standard']);
                $param['comid'] = $user['comid'];
                $model->addData($param);
            }else{
                $param['standard'] = serialize($param['standard']);
                $model->editDataBy($param,['comid'=>$user['comid']]);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '查看寺院心灯规格');
        return admin_json(1, '编辑成功');
    }
    /**
     * [restore  还原默认]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function restore()
    {
        $user = $this->user;
        try{
            $gongdeng = config('gongdeng');
        }catch (\Exception $e) {
            writelog($user['mid'], $user['comid'], $e->getMessage());
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '还原默认心灯');
        return admin_json(1, '获取成功',$gongdeng);
    }
    /**
     * [lifo  礼佛心灯]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function lifo()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        $map = [];
        try{
            if (!empty($param['status'])) {
                if($param['status'] == 1){
                    $map['c.status'] = 1;
                }else{
                    $map['c.status'] = 0;
                }
            }
            if (!empty($param['username'])) {
                $map['m.username'] = ['like', "%" . $param['username'] . "%"];
            }
            if (!empty($param['tel'])) {
                $map['m.tel'] = ['like', "%" . $param['tel'] . "%"];
            }
            if (!empty($param['sex'])) {
                $map['m.sex'] = $param['sex'];
            }
            if(!empty($param['start']) || !empty($param['end'])){
                $map['c.create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['c.comid'] = $user['comid'];
            $map['c.type'] = 1;
            $field = 'c.id,c.mid,c.gongdeng,c.day,c.name,c.to_name,c.content,c.order_num,c.total_price,c.paytime,c.status,c.states,c.bless_img,c.create_time,m.img,m.username,m.tel mtel,m.sex';
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size,$field);
            foreach($lists as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('index.domain_name').$v['img'];
                }
                if(!empty($v['paytime'])){
                    $v['endtime'] = date('Y-m-d H:i:s',$v['paytime'] + $v['day']*24*60*60);
                    $v['paytime'] = date('Y-m-d H:i:s',$v['paytime']);
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看礼佛心灯');
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [diandeng  点灯]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function diandeng()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        $user = $this->user;
        try{
            $param['status'] = 1;
            $param['states'] = 1;
            $param['paytime'] = time();
            $model->editData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '点灯成功');
        return admin_json(1, '点灯成功');
    }
}